package com.chenli.yinlianpay;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class WebviewActivity extends Activity {

    private WebView webview;
    private WebSettings mWebSettings;
    private android.view.View mErrorView;
    private RelativeLayout mErrorLayout;//错误的页面
//    private RelativeLayout mLoading_over;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        webview = (WebView) findViewById(R.id.main_webview);
//        mLoading_over = (RelativeLayout) findViewById(loading_over);
        setUpView();
    }
    private void setUpView() {
        //加载需要显示的网页
        webview.loadUrl("http://www.baidu.com/");
        //设置WebView属性，能够执行Javascript脚本
        webview.getSettings().setJavaScriptEnabled(true);
        mWebSettings = webview.getSettings();
        mWebSettings.setJavaScriptEnabled(true);  //允许加载javascript
        mWebSettings.setSupportZoom(true);     //允许缩放
        mWebSettings.setBuiltInZoomControls(true); //原网页基础上缩放
        mWebSettings.setUseWideViewPort(true);   //任意比例缩放
        webview.setWebViewClient(webClient); //设置Web视图
    }
    /***
     * 设置Web视图的方法
     */
    WebViewClient webClient = new WebViewClient(){//处理网页加载失败时


//        @Override
//        public void onFormResubmission(WebView view, Message dontResend, Message resend) {
//          //应用程序重新请求网页数据
//            webview.loadUrl("http://www.baidu.com/");
//            super.onFormResubmission(view, dontResend, resend);
//
//        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            showErrorPage();//显示错误页面
        };

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
//            mLoading_over.setVisibility(View.GONE);

        }

    };

    // 网络状态
    public boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager
                    .getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    boolean mIsErrorPage;
    protected void showErrorPage() {
        LinearLayout webParentView = (LinearLayout)webview.getParent();
        initErrorPage();//初始化自定义页面
        while (webParentView.getChildCount() > 1) {
            webParentView.removeViewAt(0);
        }
//        @SuppressWarnings("deprecation")
//        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewPager.LayoutParams.FILL_PARENT, ViewPager.LayoutParams.FILL_PARENT);
//        webParentView.addView(mErrorView, 0, lp);
        webParentView.addView(mErrorView,0);
        mIsErrorPage = true;
    }
    /****
     * 把系统自身请求失败时的网页隐藏
     */
    protected void hideErrorPage() {
        LinearLayout webParentView = (LinearLayout)webview.getParent();
        mIsErrorPage = false;
        while (webParentView.getChildCount() > 1) {
            webParentView.removeViewAt(0);
        }
    }
    /***
     * 显示加载失败时自定义的网页
     */
    protected void initErrorPage() {
        if (mErrorView == null) {
            mErrorView = View.inflate(this, R.layout.activity_error, null);
            mErrorLayout = (RelativeLayout)mErrorView.findViewById(R.id.online_error_btn_retry);
            mErrorLayout.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if(isNetworkConnected(getApplicationContext())){

                        webview.reload();
                        if(mErrorLayout!=null){
                            mErrorLayout.setVisibility(View.GONE);
//                            mLoading_over.setVisibility(View.VISIBLE);
                        }
                    }else {
                        Toast.makeText(WebviewActivity.this, "网络不可用，请联网后重试", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            mErrorView.setOnClickListener(null);
        }
    }

    //	@Override
    //设置回退
    //覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
    //	public boolean onKeyDown(int keyCode, KeyEvent event) {
    //		if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
    //			webview.goBack(); //goBack()表示返回WebView的上一页面
    //			return true;
    //		}
    //		return false;
    //	}

}

